package com.bana.fvs.controller;

import javax.annotation.Resource;

import com.bana.fvs.code.DeviceStatus;
import com.bana.fvs.dao.ControllerDAO;
import com.bana.fvs.model.Controller;
import com.bana.fvs.service.VisibilityService;
import com.bana.sys.code.DatabaseResult;
import com.bana.sys.utils.*;
import org.springframework.web.bind.annotation.*;

import com.bana.fvs.dao.VisibilityDAO;
import com.bana.fvs.model.Visibility;

import java.util.List;

/**
 * 能见度检测仪
 */
@RestController
@RequestMapping(value = "/visibility", method = RequestMethod.POST)
public class VisibilityController {

	@Resource
	private ControllerDAO controllerDAO;

	@Resource
	private VisibilityDAO visibilityDAO;

	@Resource
	private VisibilityService visibilityService;

	/**
	 * 查询雾区中的能见度检测仪集合
	 * @param request wqbm 雾区编码
	 * @return 能见度检测仪集合
	 */
	@RequestMapping(value = "/getVisibilities.do", method = RequestMethod.POST)
	public List<Visibility> getVisibilities(@RequestBody TokenRequest<Visibility> request) {
		return visibilityDAO.getVisibilities(request.getData().getWqbm());
	}

	/**
	 * 查询雾区中的能见度检测仪集合
	 * @param request wqbm 雾区编码
	 * @return 能见度检测仪集合
	 */
	@RequestMapping(value = "/queryVisibilities.do", method = RequestMethod.POST)
	public List<Visibility> queryVisibilities(@RequestBody TokenRequest<Visibility> request) {
		return visibilityService.queryVisibilities(request.getData().getWqbm());
	}

	/**
	 * 新增能见度检测仪详情
	 * @param request 能见度检测仪
	 * @return 添加结果
	 */
	@RequestMapping(value = "/insertVisibility.do",method = RequestMethod.POST)
	public ActionResult insertVisibility(@RequestBody TokenRequest<Visibility> request){
		ActionResult actionResult=new ActionResult();
		Visibility visibility=request.getData();
		visibility.setSbzt(DeviceStatus.Error);
		visibilityDAO.insertVisibility(visibility);
		if(visibility.getControllers()!=null)
		{
			for(Controller controller:visibility.getControllers())
			{
				controllerDAO.updateControllerVisibility(controller);
			}
		}
		actionResult.setCzjg(DatabaseResult.Success);
		actionResult.setKey(request.getData().getJcybm());
		return actionResult;
	}

	/**
	 * 更新能见度检测仪详情
	 * @param request 能见度检测仪
	 * @return 更新结果
	 */
	@RequestMapping(value = "/updateVisibility.do",method =RequestMethod.POST )
	public ActionResult updateVisibility(@RequestBody TokenRequest<Visibility> request){
		ActionResult actionResult=new ActionResult();
		visibilityDAO.updateVisibility(request.getData());
		if(request.getData().getControllers()!=null)
		{
			for(Controller controller:request.getData().getControllers())
			{
				controllerDAO.updateControllerVisibility(controller);
			}
		}
		actionResult.setCzjg(DatabaseResult.Success);
		return actionResult;
	}

	/**
	 * 删除能见度检测仪详情
	 * @param request 检测仪编码
	 * @return 删除结果
	 */
	@RequestMapping(value = "/deleteVisibility.do",method =RequestMethod.POST )
	public ActionResult deleteVisibility(@RequestBody TokenRequest<Visibility> request){
		ActionResult actionResult=new ActionResult();
		visibilityDAO.deleteVisibility(request.getData().getJcybm());
		actionResult.setCzjg(DatabaseResult.Success);;
		return actionResult;
	}


}
